package com.heba.banner;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.View;

import com.heba.greenmulu.R;
import com.heba.utils.DensityUtils;


/**
 * ViewPagerFocusView
 * Created by shenghao on 2015/6/01.
 */
public class ViewPagerFocusView extends View {

    private int count;                    //圆点的总数量
    private int currentIndex;        //当前焦点的位置从0开始
    private int interval;            //圆点与圆点之间的间隔
    private Bitmap losesFocus;            //没有获得焦点的圆点
    private Bitmap getFocus;            //获得焦点的圆点
    private Paint paint;

    private Context context;

    public ViewPagerFocusView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        interval = DensityUtils.dip2px(context, 5);
    }

    public ViewPagerFocusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        interval = DensityUtils.dip2px(context, 5);
    }

    public ViewPagerFocusView(Context context) {
        super(context);
        this.context = context;
        interval = DensityUtils.dip2px(context, 5);
    }

    /**
     * 設置圓點的數量
     *
     * @param count
     */
    public void setCount(int count) {
        this.count = count;
        if (count >= 0) {
            losesFocus = BitmapFactory.decodeResource(getResources(), R.drawable.guide_point_normal);
            getFocus = BitmapFactory.decodeResource(getResources(), R.drawable.guide_point_select);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.BLACK);
            paint.setTextAlign(Align.CENTER);
        }
    }

    /**
     * 設置圓點的數量和圖片
     *
     * @param count
     * @param dotNormal   默認圖片
     * @param dotSelected 選中圖片
     */
    public void setCount(int count, int dotNormal, int dotSelected) {
        this.count = count;
        if (count >= 0) {
            losesFocus = BitmapFactory.decodeResource(getResources(), dotNormal);
            getFocus = BitmapFactory.decodeResource(getResources(), dotSelected);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.BLACK);
            paint.setTextAlign(Align.CENTER);
        }
    }

    /**
     * 当前位置
     *
     * @param currentIndex currentIndex
     */
    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
        invalidate();  //刷新
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawRound(canvas);
    }

    /**
     * 画点
     *
     * @param canvas
     */
    private void drawRound(Canvas canvas) {
        if (getFocus == null) return;

        int height = getHeight();
        int ratioPaddingTop = height / 2 - getFocus.getHeight() / 2;

        //居右
//        int start = (getWidth() - (count + 1) * interval - losesFocus.getWidth() * count - paddingRightSize);//居右时的起始坐标

        int focusWidth = (count + 1) * interval + getFocus.getWidth() * count;//所有小点，加间隙的总长度
        int start = getWidth() / 2 - focusWidth / 2;//居中时起始坐标

        for (int i = 0; i < count; i++) {
            int left = (start + i * interval + interval + i * getFocus.getWidth());
            int loseLeft = (start + i * interval + interval + i * getFocus.getWidth() + losesFocus.getWidth() / 2);
            if (i == currentIndex) {
                canvas.drawBitmap(getFocus, left, ratioPaddingTop, paint);
            } else {
                if (getFocus.getWidth() > losesFocus.getWidth()) {
                    canvas.drawBitmap(losesFocus, loseLeft, ratioPaddingTop, paint);
                } else {
                    canvas.drawBitmap(losesFocus, left, ratioPaddingTop, paint);
                }
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = 20 + (getFocus != null ? getFocus.getWidth() : 0);
        setMeasuredDimension(widthMeasureSpec, measuredHeight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private boolean isEmpty(String url) {
        return url == null || "".equals(url) || "null".equals(url);
    }
}